<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Java 备忘清单
 &#x26;  java cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Java 备忘清单
===

该备忘单是针对 Java 初学者的速成课程，有助于复习 Java 语言的基本语法。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="java,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="..\style\style.css">
<link rel="stylesheet" href="..\style\katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="..\index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/java.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.4.1"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="java-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 384 512" height="1em" width="1em">
  <path d="M277.74 312.9c9.8-6.7 23.4-12.5 23.4-12.5s-38.7 7-77.2 10.2c-47.1 3.9-97.7 4.7-123.1 1.3-60.1-8 33-30.1 33-30.1s-36.1-2.4-80.6 19c-52.5 25.4 130 37 224.5 12.1zm-85.4-32.1c-19-42.7-83.1-80.2 0-145.8C296 53.2 242.84 0 242.84 0c21.5 84.5-75.6 110.1-110.7 162.6-23.9 35.9 11.7 74.4 60.2 118.2zm114.6-176.2c.1 0-175.2 43.8-91.5 140.2 24.7 28.4-6.5 54-6.5 54s62.7-32.4 33.9-72.9c-26.9-37.8-47.5-56.6 64.1-121.3zm-6.1 270.5a12.19 12.19 0 0 1-2 2.6c128.3-33.7 81.1-118.9 19.8-97.3a17.33 17.33 0 0 0-8.2 6.3 70.45 70.45 0 0 1 11-3c31-6.5 75.5 41.5-20.6 91.4zM348 437.4s14.5 11.9-15.9 21.2c-57.9 17.5-240.8 22.8-291.6.7-18.3-7.9 16-19 26.8-21.3 11.2-2.4 17.7-2 17.7-2-20.3-14.3-131.3 28.1-56.4 40.2C232.84 509.4 401 461.3 348 437.4zM124.44 396c-78.7 22 47.9 67.4 148.1 24.5a185.89 185.89 0 0 1-28.2-13.8c-44.7 8.5-65.4 9.1-106 4.5-33.5-3.8-13.9-15.2-13.9-15.2zm179.8 97.2c-78.7 14.8-175.8 13.1-233.3 3.6 0-.1 11.8 9.7 72.4 13.6 92.2 5.9 233.8-3.3 237.1-46.9 0 0-6.4 16.5-76.2 29.7zM260.64 353c-59.2 11.4-93.5 11.1-136.8 6.6-33.5-3.5-11.6-19.7-11.6-19.7-86.8 28.8 48.2 61.4 169.5 25.9a60.37 60.37 0 0 1-21.1-12.8z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#java-备忘清单"><span class="icon icon-link"></span></a>Java 备忘清单</h1><div class="wrap-body">
<p>该备忘单是针对 Java 初学者的速成课程，有助于复习 Java 语言的基本语法。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#hellojava">Hello.java</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量-variables">变量 Variables</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#原始数据类型">原始数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串-strings">字符串 Strings</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#循环-loops">循环 Loops</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组-arrays">数组 Arrays</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#交换变量-swap">交换变量 Swap</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型转换-type-casting">类型转换 Type Casting</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件语句-conditionals">条件语句 Conditionals</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用户输入">用户输入</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#java-字符串">Java 字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本的">基本的</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串连接">字符串连接</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串生成器">字符串生成器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#比较">比较</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#操纵">操纵</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#信息">信息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#不可变">不可变</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#java-数组">Java 数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#声明-declare">声明 Declare</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改-modify">修改 Modify</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#循环-读--写">循环 (读 &#x26; 写)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#loop-read">Loop (Read)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#二维数组-multidimensional-arrays">二维数组 Multidimensional Arrays</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#排序-sort">排序 Sort</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#java-条件语句">Java 条件语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符">运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-else">If else</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch">Switch</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#三元运算符">三元运算符</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#java-循环">Java 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-循环">For 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#增强的-for-循环">增强的 For 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while-循环">While 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#do-while-循环">Do While 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#继续声明">继续声明</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#中断语句">中断语句</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#java-多线程">Java 多线程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建线程">创建线程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#线程池">线程池</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#synchronized">synchronized</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#threadlocal">ThreadLocal</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#线程等待与唤醒">线程等待与唤醒</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#线程编排">线程编排</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#java-框架搜集">Java 框架搜集</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#java-集合">Java 集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#arraylist">ArrayList</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#hashmap">HashMap</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#concurrenthashmap">ConcurrentHashMap</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#hashset">HashSet</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#arraydeque">ArrayDeque</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#杂项-misc">杂项 Misc</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#访问修饰符">访问修饰符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用表达">常用表达</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注释-comment">注释 Comment</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#关键字">关键字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数学方法">数学方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#异常-trycatchfinally">异常 Try/Catch/Finally</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#反射">反射</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#util工具类">util工具类</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#io流">IO流</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#collections-工具类">Collections 工具类</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#stream-流">Stream 流</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="hellojava"><a aria-hidden="true" tabindex="-1" href="#hellojava"><span class="icon icon-link"></span></a>Hello.java</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Hello</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// 主要方法</span>
</span><span class="code-line">  <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token comment">// 输出: Hello, world!</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello, world!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>编译和运行</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ javac Hello.java
</span><span class="code-line">$ <span class="token function">java</span> Hello
</span><span class="code-line">Hello, world<span class="token operator">!</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量-variables"><a aria-hidden="true" tabindex="-1" href="#变量-variables"><span class="icon icon-link"></span></a>变量 Variables</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> num <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">float</span> floatNum <span class="token operator">=</span> <span class="token number">5.99f</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">char</span> letter <span class="token operator">=</span> <span class="token char">'D'</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">boolean</span> bool <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> site <span class="token operator">=</span> <span class="token string">"jaywcjlove.github.io"</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="原始数据类型"><a aria-hidden="true" tabindex="-1" href="#原始数据类型"><span class="icon icon-link"></span></a>原始数据类型</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->



























































<table class="show-header"><thead><tr><th>数据类型</th><th>大小</th><th>默认</th><th>范围</th></tr></thead><tbody><tr><td><code>byte</code></td><td>1 byte</td><td>0</td><td>-128 ^to^ 127</td></tr><tr><td><code>short</code></td><td>2 byte</td><td>0</td><td>-2^15^ ^to^ 2^15^-1</td></tr><tr><td><code>int</code></td><td>4 byte</td><td>0</td><td>-2^31^ ^to^ 2^31^-1</td></tr><tr><td><code>long</code></td><td>8 byte</td><td>0</td><td>-2^63^ ^to^ 2^63^-1</td></tr><tr><td><code>float</code></td><td>4 byte</td><td>0.0f</td><td><em>N/A</em></td></tr><tr><td><code>double</code></td><td>8 byte</td><td>0.0d</td><td><em>N/A</em></td></tr><tr><td><code>char</code></td><td>2 byte</td><td>\u0000</td><td>0 ^to^ 65535</td></tr><tr><td><code>boolean</code></td><td><em>N/A</em></td><td>false</td><td>true / false</td></tr></tbody></table>
<!--rehype:className=show-header-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串-strings"><a aria-hidden="true" tabindex="-1" href="#字符串-strings"><span class="icon icon-link"></span></a>字符串 Strings</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> first <span class="token operator">=</span> <span class="token string">"John"</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> last <span class="token operator">=</span> <span class="token string">"Doe"</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> name <span class="token operator">=</span> first <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> last<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>查看: <a href="#java-%E5%AD%97%E7%AC%A6%E4%B8%B2">Strings</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="循环-loops"><a aria-hidden="true" tabindex="-1" href="#循环-loops"><span class="icon icon-link"></span></a>循环 Loops</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> word <span class="token operator">=</span> <span class="token string">"QuickRef"</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">char</span> c<span class="token operator">:</span> word<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>c <span class="token operator">+</span> <span class="token string">"-"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: Q-u-i-c-k-R-e-f-</span>
</span></code></pre>
<p>查看: <a href="#java-%E5%BE%AA%E7%8E%AF">Loops</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组-arrays"><a aria-hidden="true" tabindex="-1" href="#数组-arrays"><span class="icon icon-link"></span></a>数组 Arrays</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> chars <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</span><span class="code-line">chars<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token char">'a'</span>
</span><span class="code-line">chars<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token char">'b'</span>
</span><span class="code-line"><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> letters <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"A"</span><span class="token punctuation">,</span> <span class="token string">"B"</span><span class="token punctuation">,</span> <span class="token string">"C"</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> mylist <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">boolean</span><span class="token punctuation">[</span><span class="token punctuation">]</span> answers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span></code></pre>
<p>查看: <a href="#java-%E6%95%B0%E7%BB%84">Arrays</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="交换变量-swap"><a aria-hidden="true" tabindex="-1" href="#交换变量-swap"><span class="icon icon-link"></span></a>交换变量 Swap</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1 2</span>
</span><span class="code-line"><span class="token keyword">int</span> temp <span class="token operator">=</span> a<span class="token punctuation">;</span>
</span><span class="code-line">a <span class="token operator">=</span> b<span class="token punctuation">;</span>
</span><span class="code-line">b <span class="token operator">=</span> temp<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2 1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类型转换-type-casting"><a aria-hidden="true" tabindex="-1" href="#类型转换-type-casting"><span class="icon icon-link"></span></a>类型转换 Type Casting</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// Widening</span>
</span><span class="code-line"><span class="token comment">// byte&#x3C;short&#x3C;int&#x3C;long&#x3C;float&#x3C;double</span>
</span><span class="code-line"><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">long</span> l <span class="token operator">=</span> i<span class="token punctuation">;</span>               <span class="token comment">// 10</span>
</span><span class="code-line"><span class="token comment">// Narrowing </span>
</span><span class="code-line"><span class="token keyword">double</span> d <span class="token operator">=</span> <span class="token number">10.02</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">long</span> l <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">long</span><span class="token punctuation">)</span>d<span class="token punctuation">;</span>         <span class="token comment">// 10</span>
</span><span class="code-line"><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// "10"</span>
</span><span class="code-line"><span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"10"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 10</span>
</span><span class="code-line"><span class="token class-name">Double</span><span class="token punctuation">.</span><span class="token function">parseDouble</span><span class="token punctuation">(</span><span class="token string">"10"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 10.0</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件语句-conditionals"><a aria-hidden="true" tabindex="-1" href="#条件语句-conditionals"><span class="icon icon-link"></span></a>条件语句 Conditionals</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>j <span class="token operator">==</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I get printed"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>j <span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I don't"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I also don't"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>查看: [Conditionals](#条件语句 Conditionals)</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="用户输入"><a aria-hidden="true" tabindex="-1" href="#用户输入"><span class="icon icon-link"></span></a>用户输入</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">Scanner</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> str <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">nextLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span> num <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="java-字符串"><a aria-hidden="true" tabindex="-1" href="#java-字符串"><span class="icon icon-link"></span></a>Java 字符串</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基本的"><a aria-hidden="true" tabindex="-1" href="#基本的"><span class="icon icon-link"></span></a>基本的</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> str1 <span class="token operator">=</span> <span class="token string">"value"</span><span class="token punctuation">;</span> 
</span><span class="code-line"><span class="token class-name">String</span> str2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> str3 <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串连接"><a aria-hidden="true" tabindex="-1" href="#字符串连接"><span class="icon icon-link"></span></a>字符串连接</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token string">"str"</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">;</span>     <span class="token comment">// 3str3</span>
</span><span class="code-line"><span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token string">"str"</span><span class="token punctuation">;</span>     <span class="token comment">// 6str</span>
</span><span class="code-line"><span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">"3"</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token string">"str"</span><span class="token punctuation">;</span>   <span class="token comment">// 33str</span>
</span><span class="code-line"><span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">"3"</span> <span class="token operator">+</span> <span class="token string">"3"</span> <span class="token operator">+</span> <span class="token string">"23"</span><span class="token punctuation">;</span>  <span class="token comment">// 3323</span>
</span><span class="code-line"><span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">""</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token string">"23"</span><span class="token punctuation">;</span> <span class="token comment">// 3323</span>
</span><span class="code-line"><span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">23</span><span class="token punctuation">;</span>        <span class="token comment">// 29</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="字符串生成器"><a aria-hidden="true" tabindex="-1" href="#字符串生成器"><span class="icon icon-link"></span></a>字符串生成器</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">StringBuilder</span> sb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
</span><span class="code-line"><span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>
</span><span class="code-line">└───┴───┴───┴───┴───┴───┴───┴───┴───┘
</span><span class="code-line"><span class="token number">0</span>   <span class="token number">1</span>   <span class="token number">2</span>   <span class="token number">3</span>   <span class="token number">4</span>   <span class="token number">5</span>   <span class="token number">6</span>   <span class="token number">7</span>   <span class="token number">8</span>   <span class="token number">9</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"Reference"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
</span><span class="code-line"><span class="token operator">|</span> <span class="token class-name">R</span> <span class="token operator">|</span> e <span class="token operator">|</span> f <span class="token operator">|</span> e <span class="token operator">|</span> r <span class="token operator">|</span> e <span class="token operator">|</span> n <span class="token operator">|</span> c <span class="token operator">|</span> e <span class="token operator">|</span>
</span><span class="code-line">└───┴───┴───┴───┴───┴───┴───┴───┴───┘
</span><span class="code-line"><span class="token number">0</span>   <span class="token number">1</span>   <span class="token number">2</span>   <span class="token number">3</span>   <span class="token number">4</span>   <span class="token number">5</span>   <span class="token number">6</span>   <span class="token number">7</span>   <span class="token number">8</span>   <span class="token number">9</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">sb<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
</span><span class="code-line"><span class="token operator">|</span> <span class="token class-name">R</span> <span class="token operator">|</span> e <span class="token operator">|</span> f <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>
</span><span class="code-line">└───┴───┴───┴───┴───┴───┴───┴───┴───┘
</span><span class="code-line"><span class="token number">0</span>   <span class="token number">1</span>   <span class="token number">2</span>   <span class="token number">3</span>   <span class="token number">4</span>   <span class="token number">5</span>   <span class="token number">6</span>   <span class="token number">7</span>   <span class="token number">8</span>   <span class="token number">9</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">sb<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"My "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
</span><span class="code-line"><span class="token operator">|</span> <span class="token class-name">M</span> <span class="token operator">|</span> y <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token class-name">R</span> <span class="token operator">|</span> e <span class="token operator">|</span> f <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>
</span><span class="code-line">└───┴───┴───┴───┴───┴───┴───┴───┴───┘
</span><span class="code-line"><span class="token number">0</span>   <span class="token number">1</span>   <span class="token number">2</span>   <span class="token number">3</span>   <span class="token number">4</span>   <span class="token number">5</span>   <span class="token number">6</span>   <span class="token number">7</span>   <span class="token number">8</span>   <span class="token number">9</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line">┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
</span><span class="code-line"><span class="token operator">|</span> <span class="token class-name">M</span> <span class="token operator">|</span> y <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token class-name">R</span> <span class="token operator">|</span> e <span class="token operator">|</span> f <span class="token operator">|</span> <span class="token operator">!</span> <span class="token operator">|</span>   <span class="token operator">|</span>   <span class="token operator">|</span>
</span><span class="code-line">└───┴───┴───┴───┴───┴───┴───┴───┴───┘
</span><span class="code-line"><span class="token number">0</span>   <span class="token number">1</span>   <span class="token number">2</span>   <span class="token number">3</span>   <span class="token number">4</span>   <span class="token number">5</span>   <span class="token number">6</span>   <span class="token number">7</span>   <span class="token number">8</span>   <span class="token number">9</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="比较"><a aria-hidden="true" tabindex="-1" href="#比较"><span class="icon icon-link"></span></a>比较</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> s1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"QuickRef"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</span><span class="code-line"><span class="token class-name">String</span> s2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"QuickRef"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</span><span class="code-line">s1 <span class="token operator">==</span> s2          <span class="token comment">// false</span>
</span><span class="code-line">s1<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span>     <span class="token comment">// true</span>
</span><span class="code-line"><span class="token string">"AB"</span><span class="token punctuation">.</span><span class="token function">equalsIgnoreCase</span><span class="token punctuation">(</span><span class="token string">"ab"</span><span class="token punctuation">)</span>  <span class="token comment">// true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="操纵"><a aria-hidden="true" tabindex="-1" href="#操纵"><span class="icon icon-link"></span></a>操纵</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"Abcd"</span><span class="token punctuation">;</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// ABCD</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// abcd</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// Abcd#</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"-"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// A-cd</span>
</span><span class="code-line"><span class="token string">"  abc "</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// abc</span>
</span><span class="code-line"><span class="token string">"ab"</span><span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// {'a', 'b'}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="信息"><a aria-hidden="true" tabindex="-1" href="#信息"><span class="icon icon-link"></span></a>信息</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"abcd"</span><span class="token punctuation">;</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// c</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span>     <span class="token comment">// 0</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"z"</span><span class="token punctuation">)</span>     <span class="token comment">// -1</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">// 4</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// abcd</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// cd</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// c</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"c"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// true</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string">"d"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// true</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="不可变"><a aria-hidden="true" tabindex="-1" href="#不可变"><span class="icon icon-link"></span></a>不可变</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span>
</span><span class="code-line">str<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 输出: hello</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> concat <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 输出: helloworld</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>concat<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>一旦创建就不能修改，任何修改都会创建一个新的String</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="java-数组"><a aria-hidden="true" tabindex="-1" href="#java-数组"><span class="icon icon-link"></span></a>Java 数组</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="声明-declare"><a aria-hidden="true" tabindex="-1" href="#声明-declare"><span class="icon icon-link"></span></a>声明 Declare</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a1<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</span><span class="code-line">a4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
</span><span class="code-line">a4<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
</span><span class="code-line">a4<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="修改-modify"><a aria-hidden="true" tabindex="-1" href="#修改-modify"><span class="icon icon-link"></span></a>修改 Modify</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</span><span class="code-line">a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">9</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 9</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 3</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="循环-读--写"><a aria-hidden="true" tabindex="-1" href="#循环-读--写"><span class="icon icon-link"></span></a>循环 (读 &#x26; 写)</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 2 4 6</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="loop-read"><a aria-hidden="true" tabindex="-1" href="#loop-read"><span class="icon icon-link"></span></a>Loop (Read)</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"c"</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: a b c </span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="二维数组-multidimensional-arrays"><a aria-hidden="true" tabindex="-1" href="#二维数组-multidimensional-arrays"><span class="icon icon-link"></span></a>二维数组 Multidimensional Arrays</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> matrix <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span> x <span class="token operator">=</span> matrix<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>  <span class="token comment">// 4</span>
</span><span class="code-line"><span class="token comment">// [[1, 2, 3], [4, 5]]</span>
</span><span class="code-line"><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">deepToString</span><span class="token punctuation">(</span>matrix<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> a<span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&#x3C;</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 1 2 3 4 5 6 7 </span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="排序-sort"><a aria-hidden="true" tabindex="-1" href="#排序-sort"><span class="icon icon-link"></span></a>排序 Sort</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> chars <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token char">'b'</span><span class="token punctuation">,</span> <span class="token char">'a'</span><span class="token punctuation">,</span> <span class="token char">'c'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>chars<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// [a, b, c]</span>
</span><span class="code-line"><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>chars<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="java-条件语句"><a aria-hidden="true" tabindex="-1" href="#java-条件语句"><span class="icon icon-link"></span></a>Java 条件语句</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="运算符"><a aria-hidden="true" tabindex="-1" href="#运算符"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<ul class="style-round">
<li><code>+</code> <em>(加法运算符(也用于字符串连接))</em></li>
<li><code>-</code> <em>(减法运算符)</em></li>
<li><code>*</code> <em>(乘法运算符)</em></li>
<li><code>/</code> <em>(分区运算符)</em></li>
<li><code>%</code> <em>(余数运算符)</em></li>
<li><code>=</code> <em>(简单赋值运算符)</em></li>
<li><code>++</code> <em>(增量运算符；将值增加 1)</em></li>
<li><code>--</code> <em>(递减运算符；将值减 1)</em></li>
<li><code>!</code> <em>(逻辑补码运算符；反转布尔值)</em></li>
</ul>
<!--rehype:className=style-round-->
<hr>
<ul class="cols-2 style-round">
<li><code>==</code> <em>(等于)</em></li>
<li><code>!=</code> <em>(不等于)</em></li>
<li><code>></code> <em>(比...更棒)</em></li>
<li><code>>=</code> <em>(大于或等于)</em></li>
<li><code>&#x3C;</code> <em>(少于)</em></li>
<li><code>&#x3C;=</code> <em>(小于或等于)</em></li>
</ul>
<!--rehype:className=cols-2 style-round-->
<hr>
<ul class="style-round">
<li><code>&#x26;&#x26;</code> <em>条件与</em></li>
<li><code>||</code> <em>条件或</em></li>
<li><a href="#%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6">?:</a> <em>三元(if-then-else 语句的简写)</em></li>
</ul>
<!--rehype:className=style-round-->
<hr>
<ul class="style-round">
<li><code>instanceof</code> <em>(将对象与指定类型进行比较)</em></li>
</ul>
<!--rehype:className=style-round-->
<hr>
<ul class="cols-2 style-round">
<li><code>~</code> <em>(一元按位补码)</em></li>
<li><code>&#x3C;&#x3C;</code> <em>(签名左移)</em></li>
<li><code>>></code> <em>(有符号右移)</em></li>
<li><code>>>></code> <em>(无符号右移)</em></li>
<li><code>&#x26;</code> <em>(按位与)</em></li>
<li><code>^</code> <em>(按位异或)</em></li>
<li><code>|</code> <em>(按位包含 OR)</em></li>
</ul>
<!--rehype:className=cols-2 style-round-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="if-else"><a aria-hidden="true" tabindex="-1" href="#if-else"><span class="icon icon-link"></span></a>If else</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token number">15</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>k <span class="token operator">></span> <span class="token number">20</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>k <span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="switch"><a aria-hidden="true" tabindex="-1" href="#switch"><span class="icon icon-link"></span></a>Switch</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> month <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">String</span> str<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">switch</span> <span class="token punctuation">(</span>month<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
</span><span class="code-line">    str <span class="token operator">=</span> <span class="token string">"January"</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">break</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
</span><span class="code-line">    str <span class="token operator">=</span> <span class="token string">"February"</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">break</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">:</span>
</span><span class="code-line">    str <span class="token operator">=</span> <span class="token string">"March"</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">break</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token keyword">default</span><span class="token operator">:</span>
</span><span class="code-line">    str <span class="token operator">=</span> <span class="token string">"Some other month"</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">break</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: Result March</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Result "</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="三元运算符"><a aria-hidden="true" tabindex="-1" href="#三元运算符"><span class="icon icon-link"></span></a>三元运算符</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span> max <span class="token operator">=</span> <span class="token punctuation">(</span>a <span class="token operator">></span> b<span class="token punctuation">)</span> <span class="token operator">?</span> a <span class="token operator">:</span> b<span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 输出: 20</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>max<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="java-循环"><a aria-hidden="true" tabindex="-1" href="#java-循环"><span class="icon icon-link"></span></a>Java 循环</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-循环"><a aria-hidden="true" tabindex="-1" href="#for-循环"><span class="icon icon-link"></span></a>For 循环</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 0123456789</span>
</span></code></pre>
<hr>
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> <span class="token number">3</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">,</span>j<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>j <span class="token operator">+</span> <span class="token string">"|"</span> <span class="token operator">+</span> i <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 0|0 -1|1 -2|2</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="增强的-for-循环"><a aria-hidden="true" tabindex="-1" href="#增强的-for-循环"><span class="icon icon-link"></span></a>增强的 For 循环</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> numbers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> number<span class="token operator">:</span> numbers<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 12345</span>
</span></code></pre>
<p>用于循环数组或列表</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while-循环"><a aria-hidden="true" tabindex="-1" href="#while-循环"><span class="icon icon-link"></span></a>While 循环</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">while</span> <span class="token punctuation">(</span>count <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  count<span class="token operator">++</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 01234</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="do-while-循环"><a aria-hidden="true" tabindex="-1" href="#do-while-循环"><span class="icon icon-link"></span></a>Do While 循环</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">do</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  count<span class="token operator">++</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>count <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 输出: 01234</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="继续声明"><a aria-hidden="true" tabindex="-1" href="#继续声明"><span class="icon icon-link"></span></a>继续声明</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">continue</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 01245</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="中断语句"><a aria-hidden="true" tabindex="-1" href="#中断语句"><span class="icon icon-link"></span></a>中断语句</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">break</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: 0123</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="java-多线程"><a aria-hidden="true" tabindex="-1" href="#java-多线程"><span class="icon icon-link"></span></a>Java 多线程</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="创建线程"><a aria-hidden="true" tabindex="-1" href="#创建线程"><span class="icon icon-link"></span></a>创建线程</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 实现Runnable接口</span>
</span><span class="code-line"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RunnableThread</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token annotation punctuation">@Override</span>
</span><span class="code-line">    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">       <span class="token comment">// todo something</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 实现Callable接口,T 替换成实际类型</span>
</span><span class="code-line"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CallableTask</span> <span class="token keyword">implements</span> <span class="token class-name">Callable</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token annotation punctuation">@Override</span>
</span><span class="code-line">    <span class="token keyword">public</span> <span class="token class-name">T</span> <span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
</span><span class="code-line">        <span class="token comment">// todo something</span>
</span><span class="code-line">        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 继承Thrad类</span>
</span><span class="code-line"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ExtendsThread</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token annotation punctuation">@Override</span>
</span><span class="code-line">    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">       <span class="token comment">// todo something</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 运行线程</span>
</span><span class="code-line"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ExecutionException</span><span class="token punctuation">,</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
</span><span class="code-line">        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RunnableThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">        <span class="token keyword">new</span> <span class="token class-name">ExtendsThread2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">        <span class="token class-name">FutureTask</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> integerFutureTask <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FutureTask</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">CallableTask</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">        integerFutureTask<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line">
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="线程池"><a aria-hidden="true" tabindex="-1" href="#线程池"><span class="icon icon-link"></span></a>线程池</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token doc-comment comment">/**
</span></span><span class="code-line"><span class="token doc-comment comment"> *  corePoolSize: 核心线程数
</span></span><span class="code-line"><span class="token doc-comment comment"> *  maximumPoolSize: 最大线程数
</span></span><span class="code-line"><span class="token doc-comment comment"> *  keepAliveTime: 线程空闲时间
</span></span><span class="code-line"><span class="token doc-comment comment"> *  timeUni: 线程空闲时间单位
</span></span><span class="code-line"><span class="token doc-comment comment"> *  workQueue: 线程等待队列
</span></span><span class="code-line"><span class="token doc-comment comment"> *  threadFactory: 线程创建工厂
</span></span><span class="code-line"><span class="token doc-comment comment"> *  handler: 拒绝策略
</span></span><span class="code-line"><span class="token doc-comment comment"> */</span>
</span><span class="code-line"><span class="token class-name">ThreadPoolExecutor</span> threadPoolExecutor <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor</span><span class="token punctuation">(</span>
</span><span class="code-line">                <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span>
</span><span class="code-line">                <span class="token number">5</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">,</span>
</span><span class="code-line">                <span class="token keyword">new</span> <span class="token class-name">ArrayBlockingQueue</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">                <span class="token keyword">new</span> <span class="token class-name">DefaultThreadFactory</span><span class="token punctuation">(</span><span class="token string">"pollName"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">                <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor<span class="token punctuation">.</span>CallerRunsPolicy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 内置的线程池, 不推荐生产使用</span>
</span><span class="code-line"><span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newCachedThreadPool</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newScheduledThreadPool</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newSingleThreadExecutor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-1"><div class="wrap-header h3wrap"><h3 id="synchronized"><a aria-hidden="true" tabindex="-1" href="#synchronized"><span class="icon icon-link"></span></a>synchronized</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-1-->
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 代码块</span>
</span><span class="code-line"><span class="token keyword">synchronized</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// (静态)方法</span>
</span><span class="code-line"><span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span><span class="token keyword">static</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">methodName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="threadlocal"><a aria-hidden="true" tabindex="-1" href="#threadlocal"><span class="icon icon-link"></span></a>ThreadLocal</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 使用完之后一定要记得remove, 否则会内存泄露</span>
</span><span class="code-line"><span class="token class-name">ThreadLocal</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> threadLocal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadLocal</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">threadLocal<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">threadLocal<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">threadLocal<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="线程等待与唤醒"><a aria-hidden="true" tabindex="-1" href="#线程等待与唤醒"><span class="icon icon-link"></span></a>线程等待与唤醒</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 需要synchronized修饰的代码块才能使用</span>
</span><span class="code-line"><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</span><span class="code-line"><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">notifyAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 使用lock的条件唤醒</span>
</span><span class="code-line"><span class="token class-name">ReentrantLock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">Condition</span> condition<span class="token operator">=</span> lock<span class="token punctuation">.</span><span class="token function">newCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">try</span><span class="token punctuation">{</span>
</span><span class="code-line">   <span class="token comment">// 当前线程唤醒或等待</span>
</span><span class="code-line">    condition<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">    condition<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">    condition<span class="token punctuation">.</span><span class="token function">signalAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
</span><span class="code-line">  lock<span class="token punctuation">.</span>unlock
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// LockSupport,可以先unpark,后续park不会阻塞线程</span>
</span><span class="code-line"><span class="token class-name">LockSupport</span><span class="token punctuation">.</span><span class="token function">park</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">LockSupport</span><span class="token punctuation">.</span><span class="token function">unpark</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="线程编排"><a aria-hidden="true" tabindex="-1" href="#线程编排"><span class="icon icon-link"></span></a>线程编排</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// CountDownLatch</span>
</span><span class="code-line"><span class="token class-name">CountDownLatch</span> countDownLatch <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CountDownLatch</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">try</span> <span class="token punctuation">{</span>
</span><span class="code-line">       <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</span><span class="code-line">    <span class="token punctuation">}</span><span class="token keyword">finally</span> <span class="token punctuation">{</span>
</span><span class="code-line">       countDownLatch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">countDownLatch<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// CompletableFuture</span>
</span><span class="code-line"><span class="token class-name">CompletableFuture</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Void</span><span class="token punctuation">></span></span> task1 <span class="token operator">=</span> <span class="token class-name">CompletableFuture</span><span class="token punctuation">.</span><span class="token function">runAsync</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">CompletableFuture</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Void</span><span class="token punctuation">></span></span> task2 <span class="token operator">=</span> <span class="token class-name">CompletableFuture</span><span class="token punctuation">.</span><span class="token function">runAsync</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">CompletableFuture</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Void</span><span class="token punctuation">></span></span> task3 <span class="token operator">=</span> <span class="token class-name">CompletableFuture</span><span class="token punctuation">.</span><span class="token function">runAsync</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">CompletableFuture</span><span class="token punctuation">.</span><span class="token function">allOf</span><span class="token punctuation">(</span>task1<span class="token punctuation">,</span> task2<span class="token punctuation">,</span> task3<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Semaphore</span>
</span><span class="code-line"><span class="token class-name">Semaphore</span> semaphore <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Semaphore</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">try</span> <span class="token punctuation">{</span>
</span><span class="code-line">    semaphore<span class="token punctuation">.</span><span class="token function">acquire</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
</span><span class="code-line">    semaphore<span class="token punctuation">.</span><span class="token function">release</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="java-框架搜集"><a aria-hidden="true" tabindex="-1" href="#java-框架搜集"><span class="icon icon-link"></span></a>Java 框架搜集</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="java-集合"><a aria-hidden="true" tabindex="-1" href="#java-集合"><span class="icon icon-link"></span></a>Java 集合</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->




















































































































































































































<table class="show-header"><thead><tr><th align="left">集合</th><th align="left">Interface</th><th align="left">有序</th><th align="left">已排序</th><th align="left">线程安全</th><th align="left">复制</th><th align="left">Nullable</th></tr></thead><tbody><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html">ArrayList</a></td><td align="left">List</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html">Vector</a></td><td align="left">List</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left">Y</td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html">LinkedList</a></td><td align="left">List, Deque</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html">CopyOnWriteArrayList</a></td><td align="left">List</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left">Y</td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html">HashSet</a></td><td align="left">Set</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">One <code>null</code></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html">LinkedHashSet</a></td><td align="left">Set</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">One <code>null</code></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html">TreeSet</a></td><td align="left">Set</td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html">CopyOnWriteArraySet</a></td><td align="left">Set</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">One <code>null</code></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html">ConcurrentSkipListSet</a></td><td align="left">Set</td><td align="left">Y</td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html">HashMap</a></td><td align="left">Map</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N (key)</em></td><td align="left">One <code>null</code> <em>(key)</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html">HashTable</a></td><td align="left">Map</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left"><em>N (key)</em></td><td align="left"><em>N (key)</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html">LinkedHashMap</a></td><td align="left">Map</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left"><em>N (key)</em></td><td align="left">One <code>null</code> <em>(key)</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html">TreeMap</a></td><td align="left">Map</td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N (key)</em></td><td align="left"><em>N (key)</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html">ConcurrentHashMap</a></td><td align="left">Map</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left"><em>N (key)</em></td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html">ConcurrentSkipListMap</a></td><td align="left">Map</td><td align="left">Y</td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N (key)</em></td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html">ArrayDeque</a></td><td align="left">Deque</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html">PriorityQueue</a></td><td align="left">Queue</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html">ConcurrentLinkedQueue</a></td><td align="left">Queue</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html">ConcurrentLinkedDeque</a></td><td align="left">Deque</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html">ArrayBlockingQueue</a></td><td align="left">Queue</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html">LinkedBlockingDeque</a></td><td align="left">Deque</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td></tr><tr><td align="left"><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html">PriorityBlockingQueue</a></td><td align="left">Queue</td><td align="left">Y</td><td align="left"><em>N</em></td><td align="left">Y</td><td align="left">Y</td><td align="left"><em>N</em></td></tr></tbody></table>
<!--rehype:className=show-header-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="arraylist"><a aria-hidden="true" tabindex="-1" href="#arraylist"><span class="icon icon-link"></span></a>ArrayList</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 添加</span>
</span><span class="code-line">nums<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">nums<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">nums<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检索</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 为循环迭代编制索引</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">nums<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">nums<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 非常慢</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span> value <span class="token operator">:</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// lambda 打印元素</span>
</span><span class="code-line">nums<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>e <span class="token operator">-></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="hashmap"><a aria-hidden="true" tabindex="-1" href="#hashmap"><span class="icon icon-link"></span></a>HashMap</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">></span></span> m <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">"Five"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token string">"Eight"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token string">"Six"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">"Four"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">"Two"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 检索</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// Lambda forEach</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> value<span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">String</span> msg <span class="token operator">=</span> key <span class="token operator">+</span> <span class="token string">": "</span> <span class="token operator">+</span> value<span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="concurrenthashmap"><a aria-hidden="true" tabindex="-1" href="#concurrenthashmap"><span class="icon icon-link"></span></a>ConcurrentHashMap</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">></span></span> m
</span><span class="code-line">            <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConcurrentHashMap</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">"Hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">101</span><span class="token punctuation">,</span> <span class="token string">"Geeks"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token number">102</span><span class="token punctuation">,</span> <span class="token string">"Geeks"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 移除</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">101</span><span class="token punctuation">,</span> <span class="token string">"Geeks"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 如果不存在，就添加，存在就不变更</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">putIfAbsent</span><span class="token punctuation">(</span><span class="token number">103</span><span class="token punctuation">,</span> <span class="token string">"Hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token comment">// 替换</span>
</span><span class="code-line">m<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token number">101</span><span class="token punctuation">,</span> <span class="token string">"Hello"</span><span class="token punctuation">,</span> <span class="token string">"For"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="hashset"><a aria-hidden="true" tabindex="-1" href="#hashset"><span class="icon icon-link"></span></a>HashSet</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>set<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Empty!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"dog"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"cat"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"mouse"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"snake"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"bear"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>set<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"cat"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Contains cat"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"cat"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> element <span class="token operator">:</span> set<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">set<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>e <span class="token operator">-></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="arraydeque"><a aria-hidden="true" tabindex="-1" href="#arraydeque"><span class="icon icon-link"></span></a>ArrayDeque</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">Deque</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayDeque</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用 add()</span>
</span><span class="code-line">a<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"Dog"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用 addFirst()</span>
</span><span class="code-line">a<span class="token punctuation">.</span><span class="token function">addFirst</span><span class="token punctuation">(</span><span class="token string">"Cat"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 使用 addLast()</span>
</span><span class="code-line">a<span class="token punctuation">.</span><span class="token function">addLast</span><span class="token punctuation">(</span><span class="token string">"Horse"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// [Cat, Dog, Horse]</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 访问元素</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 移除元素</span>
</span><span class="code-line"><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="杂项-misc"><a aria-hidden="true" tabindex="-1" href="#杂项-misc"><span class="icon icon-link"></span></a>杂项 Misc</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="访问修饰符"><a aria-hidden="true" tabindex="-1" href="#访问修饰符"><span class="icon icon-link"></span></a>访问修饰符</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->








































<table class="show-header"><thead><tr><th>修饰符</th><th>Class</th><th>Package</th><th>Subclass</th><th>World</th></tr></thead><tbody><tr><td>public</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td></tr><tr><td>protected</td><td>Y</td><td>Y</td><td>Y</td><td><em>N</em></td></tr><tr><td>no modifier</td><td>Y</td><td>Y</td><td><em>N</em></td><td><em>N</em></td></tr><tr><td>private</td><td>Y</td><td><em>N</em></td><td><em>N</em></td><td><em>N</em></td></tr></tbody></table>
<!--rehype:className=show-header-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="常用表达"><a aria-hidden="true" tabindex="-1" href="#常用表达"><span class="icon icon-link"></span></a>常用表达</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token class-name">String</span> text <span class="token operator">=</span> <span class="token string">"I am learning Java"</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 删除所有空格</span>
</span><span class="code-line">text<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">"\\s+"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 拆分字符串</span>
</span><span class="code-line">text<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"\\|"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">text<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">quote</span><span class="token punctuation">(</span><span class="token string">"|"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
<p>查看: <a href="./regex.html#java-%E4%B8%AD%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F">Regex in java</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="注释-comment"><a aria-hidden="true" tabindex="-1" href="#注释-comment"><span class="icon icon-link"></span></a>注释 Comment</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 我是单行注释！</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token comment">/*
</span></span><span class="code-line"><span class="token comment">而我是一个
</span></span><span class="code-line"><span class="token comment">多行注释！
</span></span><span class="code-line"><span class="token comment">*/</span>
</span><span class="code-line"><span class="token doc-comment comment">/**
</span></span><span class="code-line"><span class="token doc-comment comment">  * 这个
</span></span><span class="code-line"><span class="token doc-comment comment">  * 是
</span></span><span class="code-line"><span class="token doc-comment comment">  * 文档
</span></span><span class="code-line"><span class="token doc-comment comment">  * 注释
</span></span><span class="code-line"><span class="token doc-comment comment"> */</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="关键字"><a aria-hidden="true" tabindex="-1" href="#关键字"><span class="icon icon-link"></span></a>关键字</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<ul class="cols-7 style-none">
<li>abstract</li>
<li>continue</li>
<li>for</li>
<li>new</li>
<li>switch</li>
<li>assert</li>
<li>default</li>
<li>goto</li>
<li>package</li>
<li>synchronized</li>
<li>boolean</li>
<li>do</li>
<li>if</li>
<li>private</li>
<li>this</li>
<li>break</li>
<li>double</li>
<li>implements</li>
<li>protected</li>
<li>throw</li>
<li>byte</li>
<li>else</li>
<li>import</li>
<li>public</li>
<li>throws</li>
<li>case</li>
<li>enum</li>
<li>instanceof</li>
<li>return</li>
<li>transient</li>
<li>catch</li>
<li>extends</li>
<li>int</li>
<li>short</li>
<li>try</li>
<li>char</li>
<li>final</li>
<li>interface</li>
<li>static</li>
<li>void</li>
<li>class</li>
<li>finally</li>
<li>long</li>
<li>strictfp</li>
<li>volatile</li>
<li>const</li>
<li>float</li>
<li>native</li>
<li>super</li>
<li>while</li>
</ul>
<!--rehype:className=cols-7 style-none-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数学方法"><a aria-hidden="true" tabindex="-1" href="#数学方法"><span class="icon icon-link"></span></a>数学方法</h3><div class="wrap-body">





























































<table><thead><tr><th align="left">方法</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left"><code>Math.max(a,b)</code></td><td align="left"><code>a</code> 和 <code>b</code> 的最大值</td></tr><tr><td align="left"><code>Math.min(a,b)</code></td><td align="left"><code>a</code> 和 <code>b</code> 的最小值</td></tr><tr><td align="left"><code>Math.abs(a)</code></td><td align="left">绝对值</td></tr><tr><td align="left"><code>Math.sqrt(a)</code></td><td align="left"><code>a</code> 的平方根</td></tr><tr><td align="left"><code>Math.pow(a,b)</code></td><td align="left"><code>b</code> 的幂</td></tr><tr><td align="left"><code>Math.round(a)</code></td><td align="left">最接近的整数</td></tr><tr><td align="left"><code>Math.sin(ang)</code></td><td align="left">正弦</td></tr><tr><td align="left"><code>Math.cos(ang)</code></td><td align="left"><code>ang</code> 的余弦</td></tr><tr><td align="left"><code>Math.tan(ang)</code></td><td align="left"><code>ang</code> 的切线</td></tr><tr><td align="left"><code>Math.asin(ang)</code></td><td align="left"><code>ang</code> 的反正弦</td></tr><tr><td align="left"><code>Math.log(a)</code></td><td align="left"><code>a</code> 的自然对数</td></tr><tr><td align="left"><code>Math.toDegrees(rad)</code></td><td align="left">以度为单位的角度弧度</td></tr><tr><td align="left"><code>Math.toRadians(deg)</code></td><td align="left">以弧度为单位的角度度</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="异常-trycatchfinally"><a aria-hidden="true" tabindex="-1" href="#异常-trycatchfinally"><span class="icon icon-link"></span></a>异常 Try/Catch/Finally</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token keyword">try</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// something</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"always printed"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="反射"><a aria-hidden="true" tabindex="-1" href="#反射"><span class="icon icon-link"></span></a>反射</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token doc-comment comment">/**
</span></span><span class="code-line"><span class="token doc-comment comment">* 利用反射动态加载依赖库
</span></span><span class="code-line"><span class="token doc-comment comment">* java9及以上版本可用
</span></span><span class="code-line"><span class="token doc-comment comment">* <span class="token keyword">@param</span> <span class="token parameter">jar</span> jar文件
</span></span><span class="code-line"><span class="token doc-comment comment">*/</span>
</span><span class="code-line"><span class="token class-name">Method</span> method <span class="token operator">=</span> <span class="token class-name">URLClassLoader</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">"addURL"</span><span class="token punctuation">,</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">method<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">MethodHandle</span> addURL <span class="token operator">=</span> lookup<span class="token punctuation">.</span><span class="token function">unreflect</span><span class="token punctuation">(</span>method<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">URL</span> url <span class="token operator">=</span> jar<span class="token punctuation">.</span><span class="token function">toURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toURL</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">URLClassLoader</span> urlClassLoader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">URLClassLoader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">URL</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span>url<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">addURL<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>urlClassLoader<span class="token punctuation">,</span> url<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">//java8</span>
</span><span class="code-line"><span class="token class-name">Method</span> method <span class="token operator">=</span> <span class="token class-name">URLClassLoader</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">"addURL"</span><span class="token punctuation">,</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">method<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>classLoader<span class="token punctuation">,</span> url<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="util工具类"><a aria-hidden="true" tabindex="-1" href="#util工具类"><span class="icon icon-link"></span></a>util工具类</h3><div class="wrap-body">
<ul>
<li><code>ArrayDeque</code> 提供 resizable-array 并实现 Deque 接</li>
<li><code>Arrays</code> 包含一个静态工厂，允许将数组视为列表</li>
<li><code>Collections</code> 包含对集合进行操作或返回集合的静态方法</li>
<li><code>Date</code> 表示特定的时间瞬间，精度为毫秒</li>
<li><code>Dictionary</code> 是任何类的抽象父类，例如 Hashtable，它将键映射到值</li>
<li><code>EnumMap</code> 一个专门用于枚举键的 Map 实现</li>
<li><code>EnumSet</code> 一个专门用于枚举键的 Set 实现</li>
<li><code>Formatter</code> 提供对布局对齐和对齐、数字、字符串和日期/时间数据的常用格式以及特定于语言环境的输出的支持</li>
<li><code>SecureRandom</code> 实例用于生成安全的伪随机数流</li>
<li><code>UUID</code> 表示一个不可变的通用唯一标识符</li>
<li><code>Vector</code> 实现了一个可增长的对象数组</li>
<li><code>LocalDate</code> 表示没有时区的日期，只包含年月日，不可变并且线程安全的，java8 及以上版本可用</li>
<li><code>LocalTime</code> 表示没有时区的时间，只包含时分秒，不可变并且线程安全的，java8 及以上版本可用</li>
<li><code>LocalDateTime</code> 表示没有时区的日期时间，同时包含年月日时分秒，不可变并且线程安全的，java8 及以上版本可用</li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="io流"><a aria-hidden="true" tabindex="-1" href="#io流"><span class="icon icon-link"></span></a>IO流</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 输入流转输出流</span>
</span><span class="code-line"><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> inputContent <span class="token operator">=</span> <span class="token string">"test"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">try</span> <span class="token punctuation">(</span><span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ByteArrayInputStream</span><span class="token punctuation">(</span>inputContent<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">     <span class="token class-name">OutputStream</span> outputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ByteArrayOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buffer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">int</span> len<span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>len <span class="token operator">=</span> inputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buffer<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">        outputStream<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>buffer<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="collections-工具类"><a aria-hidden="true" tabindex="-1" href="#collections-工具类"><span class="icon icon-link"></span></a>Collections 工具类</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 计算出现频率</span>
</span><span class="code-line"><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">int</span> frequency <span class="token operator">=</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">frequency</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// frequency = 2</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="stream-流"><a aria-hidden="true" tabindex="-1" href="#stream-流"><span class="icon icon-link"></span></a>Stream 流</h3><div class="wrap-body">
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"><span class="token comment">// 统计词频</span>
</span><span class="code-line"><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&#x3C;</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">Long</span><span class="token punctuation">></span></span> frequencyMap <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">groupingBy</span><span class="token punctuation">(</span>x <span class="token operator">-></span> x<span class="token punctuation">,</span> <span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">counting</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token comment">// 1: 2</span>
</span><span class="code-line"><span class="token comment">// 2: 3</span>
</span><span class="code-line"><span class="token comment">// 3: 1</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://www.oracle.com/cn/java/">Java 官网</a> <em>(oracle.com/cn/java)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="..\/data.js?v=1.4.1" defer></script><script src="..\/js/fuse.min.js?v=1.4.1" defer></script><script src="..\/js/main.js?v=1.4.1" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
